package com.acerola.yuan_back.controller;

import com.acerola.yuan_back.common.Result;
import com.acerola.yuan_back.dao.TodoMapper;
import com.acerola.yuan_back.pojo.Todo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: yuan_back
 * @description: 待办事项控制层
 * @author: Acerola
 * @create: 2022-07-30 21:15
 **/
@Api(tags = "待办事项控制层")
@CrossOrigin
@RestController
@RequestMapping("/todo")
public class TodoController {

    @Autowired
    private TodoMapper todoMapper;

    @ApiOperation("获取所有待办事项")
    @GetMapping("/list")
    private Result<?> list(){
        return new Result<>().success().put(todoMapper.selectList(new QueryWrapper<Todo>().eq("is_done",0)));
    }
    @ApiOperation("添加待办事项")
    @PostMapping ("/add")
    private Result<?> add(@RequestBody Todo todo){
        todo.setIsDone(0);
        Integer save = todoMapper.insert(todo);
        if (save!=0){
            return new Result<>().success("添加成功");
        }else {
            return new Result<>().error("添加失败");
        }
    }

    @ApiOperation("移除待办事项")
    @PostMapping("/remove")
    private Result<?> remove(Integer id){
        Todo todo = todoMapper.selectById(id);
        todo.setIsDone(1);
        Integer b = todoMapper.updateById(todo);
        if (b!=0){
            return new Result<>().success("修改成功");
        }else {
            return new Result<>().error("修改失败");
        }
    }

}
